// Do not remove the include below
#include "EurekaIRCar.h"
#include <EurekaIR.h>
#include <EurekaIR_NEC21_38kHz.h>
#include <IRlib.h>
#include <Motor.h>
#include <Servo.h>
#include <Ultrasonic.h>

EurekaIR controle;
Ultrasonic ultrasom(12, 13);
//int led = 13;
int pinServo = 9;
Servo servo;
int posServo = 90;
int onOff = false;
int speed = 100;
Motor motor;

//The setup function is called once at startup of the sketch
void setup() {
	Serial.begin(115200);
	controle = EurekaIR(11);
	//pinMode(13, OUTPUT);
	servo.attach(pinServo);
	servo.write(posServo);
}

// The loop function is called in an endless loop
void loop() {
	long IRComando = controle.getComando();

	if (IRComando) {
		switch (IRComando) {
		case ONOFF:
			if (onOff) {
				//digitalWrite(led, HIGH);
			} else {
				//digitalWrite(led, LOW);
				motor.off(motor.Motor_LR);
			}
			onOff = !onOff;
			break;
		case BUTTON_2:
			motor.onFwd(motor.Motor_LR, speed);
			break;
		case BUTTON_8:
			motor.onRev(motor.Motor_LR, speed);
			break;
		case BUTTON_4:
			motor.turnLeft(speed);
			delay(250);
			motor.off(motor.Motor_LR);
			break;
		case BUTTON_6:
			motor.turnRight(speed);
			delay(250);
			motor.off(motor.Motor_LR);
			break;

		case POSITIVO:
			posServo = min(180,posServo+=5);
			Serial.print(posServo);
			Serial.print(" - ");
			Serial.println(ultrasom.Ranging(CM));
			servo.write(posServo);
			break;
		case NEGATIVO:
			posServo = max(0,posServo-=5);
			Serial.print(posServo);
			Serial.print(" - ");
			Serial.println(ultrasom.Ranging(CM));
			servo.write(posServo);
			break;

		}

	}
}
